﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using wfDesigner.Model;
using Microhard.WorkflowDataAccess;


namespace wfDesigner
{
    /// <summary>
    /// AddFlowTypeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddFlowChartWindow : Window
    {
        List<MicroHard_Resources> Resourcelist = new List<MicroHard_Resources>();
        List<MicroHard_Operation> Operationlist = new List<MicroHard_Operation>();
       

        public AddFlowChartWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataTimeTxt.Text = System.DateTime.Now.ToString("yyyy-MM-dd  HH:mm:ss");
            string DATE = System.DateTime.Now.Date.ToString().Substring(0,10);
            this.VersionTxt.Text = DATE + ".1";

            WorkflowDBDataContext entity = new WorkflowDBDataContext();
            var typeList = entity.t_WorkFlowTypes.Select(n => new { n.TypeId, n.TypeName });

            List<FlowType> list = new List<FlowType>();

            foreach (var t in typeList)
            {
                FlowType type = new FlowType();
                type.TypeID = t.TypeId;
                type.TypeName = t.TypeName;

                list.Add(type);
            }

            this.comboBox1.ItemsSource = list;

            OugDBDataContext entity2 = new OugDBDataContext();
            Resourcelist = entity2.MicroHard_Resources.ToList();
            this.comboBox2.ItemsSource = Resourcelist;
          
            Operationlist =entity2.MicroHard_Operation.ToList();
            this.comboBox3.ItemsSource = Operationlist;
        }

        private void AddBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                WorkflowDBDataContext entity = new WorkflowDBDataContext();
                t_FlowChart ft = new t_FlowChart();
                ft.FlowChartId = Guid.NewGuid();
                ft.FlowChartName = this.FlowNameTxt.Text;

                
                ft.ResourceID = new Guid(this.comboBox2.SelectedValue.ToString());
                var resource = Resourcelist.Where(n => n.ResourceID == ft.ResourceID).SingleOrDefault();
                ft.ResourceName = resource.Name;
                ft.ResourceKeyName = resource.KeyName;

                ft.OperationID = new Guid(this.comboBox3.SelectedValue.ToString());
                var operation = Operationlist.Where(n => n.OperationID == ft.OperationID).SingleOrDefault();
              ft.OperationName = operation.Name;
                ft.OperationKeyName = operation.KeyName;
            
                ft.Creator = this.CreatorTxt.Text;
                ft.CreateDate = Convert.ToDateTime(this.DataTimeTxt.Text);
                ft.Version = this.VersionTxt.Text;            
                
               ft.TypeId = new Guid( this.comboBox1.SelectedValue.ToString());
                ft.Status = 0;
                entity.t_FlowCharts.InsertOnSubmit(ft);
                entity.SubmitChanges();
            }
            catch
            {               
                MessageBox.Show("添加失败！", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                this.Close();
            }
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
